package com.campus.controller;

import com.campus.entry.ManagerClass;
import com.campus.entry.ManagerStudent;
import com.campus.pojo.ManagerClassDto;
import com.campus.pojo.StudentClassDto;
import com.campus.response.ResponseResult;
import com.campus.response.ResultCode;
import com.campus.service.ManagerClassService;
import com.campus.service.ManagerStudentService;
import com.campus.service.ManagerTeacherService;
import com.campus.service.TeacherStudentInClassService;
import com.campus.util.GetUserIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * 教学班模块
 * @author 荧moon
 * @date 2024/12/29
 */
@RestController
@RequestMapping("/class")
public class ManagerClassController {
    @Autowired
    private ManagerClassService managerClassService;
    @Autowired
    private TeacherStudentInClassService teacherStudentInClassService;
    @Autowired
    private ManagerTeacherService managerTeacherService;
    @Autowired
    private ManagerStudentService managerStudentService;

    /**
     * 插入班级信息
     * @param managerClass
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @PostMapping("/save")
    public ResponseResult<Object> saveManagerClass(@RequestBody ManagerClass managerClass){
        managerClass.setCreatedTime(LocalDateTime.now());
        managerClassService.createClass(managerClass);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,"null");
    }

    /**
     * 更新班级信息
     * @param managerClass
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @PutMapping("/update")
    public ResponseResult<Object> updateManagerClass(@RequestBody ManagerClass managerClass){
        managerClassService.updateClass(managerClass);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,"null");
    }

    /**
     * 班级关联老师
     * @param classId
     * @param teacherId
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")

    @PostMapping("/addTeacher")
    public ResponseResult<Object> addTeacher(@RequestParam Integer classId, @RequestParam Integer teacherId){
        managerClassService.addTeacher(classId,teacherId);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,"null");
    }

    /**
     * 班级管理学生
     * @param classId
     * @param hashMap
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @PostMapping("/addStudent/{classId}")
    public ResponseResult<Object> addStudents(@PathVariable Integer classId,
                                              @RequestBody HashMap<String,List<Integer>> hashMap){
        List<Integer> list = hashMap.get("studentIds");
        teacherStudentInClassService.addStudent(classId,list);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,"null");

    }

    /**
     * 查询班级列表
     * @return {@link ResponseResult }<{@link List }<{@link ManagerClassDto }>>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @GetMapping("/list")
    public ResponseResult<List<ManagerClassDto>> getManagerClass(){
        List<ManagerClassDto> managerClass = managerClassService.getManagerClass();
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,managerClass);

    }

    /**
     * 根据教师Id查询班级信息
     * @return {@link ResponseResult }<{@link List }<{@link ManagerClassDto }>>
     */
    @PreAuthorize("hasAnyRole({'admin','teacher'})")
    @GetMapping("/listClassByTeacherId")
    public ResponseResult<List<ManagerClassDto>> getManagerClassById(){
        Integer teacherId=managerTeacherService.getTeacherIdByLoginId(GetUserIdUtil.getUserId());
        List<ManagerClassDto> managerClassDtos=managerClassService.getManagerClassByTeacherId(teacherId);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,managerClassDtos);


    }

    /**
     * 根据班级Id查询班级信息
     * @param id
     * @return {@link ResponseResult }<{@link ManagerClass }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @GetMapping("/getClassById/{id}")
    public ResponseResult<ManagerClass> getClassById(@PathVariable Integer id){
        ManagerClass managerClass=managerClassService.getManagerClassById(id);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,managerClass);
    }

    /**
     * 根据Id删除班级信息
     * @param id
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @DeleteMapping("/{id}")
    public ResponseResult<Object> removeManagerClass(@PathVariable Integer id){
        managerClassService.removeClassById(id);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 管理员根据班级查询学生列表
     * @param id
     * @return {@link ResponseResult }<{@link List }<{@link ManagerStudent }>>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @GetMapping("/getStudentClassId/{id}")
    public ResponseResult<List<ManagerStudent>> getStudents(@PathVariable Integer id){
        List<ManagerStudent> studentsByClassid = managerClassService.getStudentsByClassid(id);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,studentsByClassid);
    }

    /**
     * 教师根据班级查询学生列表
     * @param id
     * @return {@link ResponseResult }<{@link List }<{@link ManagerStudent }>>
     */
    @PreAuthorize("hasAnyRole({'teacher'})")
    @GetMapping("/getStudent/{id}")
    public ResponseResult<List<ManagerStudent>> getStudentByTeacherId(@PathVariable Integer id){
        Integer teacherIdByLoginId = managerTeacherService.getTeacherIdByLoginId(GetUserIdUtil.getUserId());
        List<ManagerStudent> studentByClassId = managerClassService.getStudentByClassId(id,teacherIdByLoginId);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,studentByClassId);
    }

    /**
     * 学生查看班级信息
     * @return {@link ResponseResult }<{@link List }<{@link StudentClassDto }>>
     */
    @PreAuthorize("hasAnyRole({'student'})")
    @GetMapping("/getManagerClassByStudent")
    public ResponseResult<List<StudentClassDto>> getClassByStudentId(){
        System.out.println(GetUserIdUtil.getUserId());
        Integer studentId=managerStudentService.getStudentIdByLoinId(GetUserIdUtil.getUserId());
        System.out.println(studentId);
        List<StudentClassDto> studentClassDtos=managerClassService.getClassByStudent(studentId);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,studentClassDtos);
    }


}
